const formatTime = (date, type = '-') => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  // if (arguments[1] !== undefined) {
  //   return [year, month, day].map(formatNumber).join('/');
  // }
  //  else {
  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  );
  // }
};

const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : '0' + n;
};

var arraySum = function (ar) {
  var sum = 0;

  for (var i in ar) {
    sum += parseFloat(ar[i]);
  }

  return sum;
};

var formatDate = function (time, format = 'YY-MM-DD hh:mm:ss') {
  var date = new Date(time);
  var year = date.getFullYear(),
    month = date.getMonth() + 1,
    //月份是从0开始的
    day = date.getDate(),
    hour = date.getHours(),
    min = date.getMinutes(),
    sec = date.getSeconds();
  var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
    return '0' + index;
  }); ////开个长度为10的数组 格式为 00 01 02 03

  var newTime = format
    .replace(/YY/g, year)
    .replace(/MM/g, preArr[month] || month)
    .replace(/DD/g, preArr[day] || day)
    .replace(/hh/g, preArr[hour] || hour)
    .replace(/mm/g, preArr[min] || min)
    .replace(/ss/g, preArr[sec] || sec);
  return newTime;
}; // 显示繁忙提示

const formatDateOnlyTime = function (time) {
  if (!time) {
    return '';
  }

  const date = new Date(time);
  const hour = date.getHours();
  const min = date.getMinutes();

  return `${hour >= 10 ? hour : '0' + hour}:${min >= 10 ? min : '0' + min}`;
};

var showBusy = text => console.log(text);

// 显示成功提示

var showSuccess = text => console.log(text); // 显示失败提示

var showFail = text => console.log(text); // 显示失败提示

var showModal = (title, content) => {
  console.log(title);
};

function tow(n) {
  return n >= 0 && n < 10 ? '0' + n : '' + n;
}

var getDate = function (second) {
  ///倒计时
  // second = second*1000;
  var day = Math.floor(second / 86400);
  second = second % 86400; //余数代表剩下的秒数；

  var hour = Math.floor(second / 3600);
  second %= 3600; //余数代表 剩下的秒数；

  var minute = Math.floor(second / 60);
  second %= 60;
  console.log([tow(day), tow(hour), tow(minute), tow(second)]);
  return [tow(day), tow(hour), tow(minute), tow(second)];
}; //强制转价格，两位小数

var toPrice = x => {
  var f = parseFloat(x);

  if (isNaN(f)) {
    x = 0;
  }

  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');

  if (rs < 0) {
    rs = s.length;
    s += '.';
  }

  while (s.length <= rs + 2) {
    s += '0';
  }

  return s;
}; //强制转为数字

var toNum = x => {
  var f = parseInt(x);

  if (isNaN(f)) {
    f = 0;
  }

  return f;
}; //日，分，秒：前缀补0

var fixTime = x => {
  var f = toNum(x);

  if (f < 10) {
    f = '0' + f;
  }

  return f;
};
//去除空格

var trim = x => {
  if (x == undefined) {
    x = '';
  }
  x = x.toString();
  return x.replace(/(^\s*)|(\s*$)/g, '');
};
// 计算对象得长度
var getObjNum = obj => {
  let num = 0;
  for (let key in obj) {
    num += 1;
  }
  return num;
}; //替换目标字符串

// 车牌号正则
var isBusNumber = x => {
  x = trim(x);
  if (x == '') {
    return false;
  } else {
    if (
      !/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(
        x,
      )
    ) {
      return false;
    }
    return true;
  }
};
// 车架号
var isVin = vin => {
  if (vin.length > 0 && vin.length != 17) {
    return false;
  } else {
    var vinVal = vin.toUpperCase();
    //document.getElementById("vin_"+k).value = vinVal;
    var charToNum = {
      A: 1,
      B: 2,
      C: 3,
      D: 4,
      E: 5,
      F: 6,
      G: 7,
      H: 8,
      J: 1,
      K: 2,
      L: 3,
      M: 4,
      N: 5,
      P: 7,
      R: 9,
      S: 2,
      T: 3,
      U: 4,
      V: 5,
      W: 6,
      X: 7,
      Y: 8,
      Z: 9,
    };
    var obj = 0;
    var arr = new Array();
    for (var i = 0; i < vinVal.length; i++) {
      var temp = vinVal.charAt(i);

      if (charToNum[temp]) {
        arr[i] = charToNum[temp];
      } else {
        arr[i] = Number(temp);
      }
      if (i == 8) {
        arr[i] = vinVal.charAt(i);
      }
    }
    var a1 = 8;
    for (var i = 0; i < 7; i++) {
      obj += Number(arr[i]) * a1;
      a1--;
    }

    obj += Number(arr[7]) * 10;

    var a2 = 9;
    for (var i = 9; i < 17; i++) {
      obj += Number(arr[i]) * a2;
      a2--;
    }

    var result = Number(obj) % 11;
    if (parseInt(result) === 10) {
      result = 'X';
    }
    if (result == arr[8]) {
      //成功
      return true;
    } else {
      //失败
      return false;
    }
  }
};
//判断是否空对象
var replacex = (str, x, y) => {
  x = x.toString();
  return str.replace(new RegExp(x, 'gm'), y);
}; //判断是否为空

var isN = x => {
  if (x == null || x == undefined) {
    return true;
  }

  return trim(x) == '';
}; //判断是否空对象

var isEmptyObject = obj => {
  for (var key in obj) {
    return false;
  }
  return true;
};

//判断是否空数组
var isEmptyArray = array => {
  return !(array.length > 0);
};
var isIdcard = value => {
  // return /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/.test(value)
  return /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}[\dXx]$/.test(
    value,
  );
};

//判断是否手机号
var isMobile = x => {
  if (x == '') {
    return false;
  } else {
    if (
      !/^0{0,1}(13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9]|14[0-9])[0-9]{8}$/.test(
        x,
      )
    ) {
      return false;
    }

    return true;
  }
};

//判断是否邮箱
var isEmail = x => {
  if (x == '') {
    return false;
  } else {
    if (!/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/.test(x)) {
      return false;
    }
  }

  return true;
}; //数组去重除空

function trimArray(array) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] == '' || typeof array[i] === 'undefined') {
      array.splice(i, 1);
      i = i - 1;
    }
  }

  return array;
} //解析配置文件

function getIndex($arr, $id) {
  for (var $i in $arr) {
    if ($arr[$i].id == $id) {
      return $i;
    }
  }

  return -1;
}

function getFirst($obj) {
  for (var i in $obj) {
    return i;
  }
} //获取QueryString的数组

function getParams($str) {
  if (!$str) {
    return [];
  }

  var result = $str.match(new RegExp('[?&][^?&]+=[^?&]+', 'g'));

  for (var i = 0; i < result.length; i++) {
    result[i] = result[i].substring(1);
  }

  return result;
} //根据QueryString参数名称获取值

function getParamsByName($str, name) {
  if (!$str) {
    return '';
  }

  var result = $str.match(new RegExp('[?&]' + name + '=([^&]+)', 'i'));

  if (result == null || result.length < 1) {
    return '';
  }

  return result[1];
}

module.exports = {
  formatTime,
  formatDate,
  formatDateOnlyTime,
  showBusy,
  showSuccess,
  showFail,
  showModal,
  toPrice,
  toNum,
  fixTime,
  isMobile,
  getObjNum,
  isIdcard,
  isEmail,
  trim,
  isN,
  isEmptyObject,
  isEmptyArray,
  trimArray,
  isBusNumber,
  isVin,
  getDate,
  getIndex,

  getFirst,
  arraySum,
  getParams,
  getParamsByName,
};
